import HttpService from '../../service/HttpService'
import ApiUrl from '../../service/ApiUrl'
import Vue from 'vue'
// import { deepCopy } from '../../service/Util'
import Util from '../../service/Util'

let loading = Util.loading();
// mutations-type config
const Types = {
	GET_SUBJECT_REQUEST: 'GET_SUBJECT_REQUEST',
	GET_SUBJECT_ITEM_SUCCESS: 'GET_SUBJECT_ITEMS_SUCCESS',
	GET_SUBJECT_ITEM_FAILED: 'GET_SUBJECT_ITEMS_FAILED',
}

// init state
const state = {
    subjectItem: {}
}

const getters = {
    subjectItem: state => state.subjectItem
}

// action
const actions = {
	getSubjectItem ({commit, state}, subjectId){
		commit(Types.GET_SUBJECT_REQUEST)
		let opts = {
			apiObj : ApiUrl.getSubjectItem,
			path : {
				id : subjectId
			}
		};
		HttpService.accessAPI(opts, result => { 
			commit( Types.GET_SUBJECT_ITEM_SUCCESS, {result})
		}, reason => { commit(Types.GET_SUBJECT_ITEM_FAILED, { reason }) });
	},
	clearSubjectItem( {commit, state} ){
		commit(Types.GET_SUBJECT_REQUEST)
	}
}

// init mutations 
const mutations = {
	[Types.GET_SUBJECT_REQUEST] ( state ){
		state.subjectItems = {};
		loading.start();
	},
	[Types.GET_SUBJECT_ITEM_SUCCESS] ( state, { result } ){
		console.log( 'get subject items success', result );
		if( result.banner ){
	    	result.banner += '?imageView2/2/w/700';
	    }else{
	    	result.banner = '/static/placeholder.png';
	    }
	    result.goods.map(function(value){
		    if( value.cover ){
		    	value.cover += '?imageView2/2/w/700';
		    }else{
		    	value.cover = '/static/placeholder.png';
		    }
	    })
		state.subjectItem = result;
		loading.close();
	},
	[Types.GET_SUBJECT_ITEM_FAILED] ( state, { result } ){
		state.productDetail = {};
		loading.close();
		console.log( 'mutations failed', result );
	}
}

export default {
  state,
  getters,
  actions,
  mutations
}